package com.itheima.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.itheima.service.KwService;

public class KwServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//解决乱码
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//获取用户录入的值
			String kwname = request.getParameter("kwname");
			System.out.println("kwname="+kwname);
			//调用service完成查询
			KwService ks = new KwService();
			List<Object> list=ks.findList(kwname);
			
			//把list转换为json
			Gson g = new Gson();
			String json = g.toJson(list);
			System.out.println(json);
			//写回jsp
			response.getWriter().print(json);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}